package com.wbao.dianniu.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lhh.apst.library.CustomPagerSlidingTabStrip2;
import com.lhh.apst.library.ViewHolder;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.tabstrip.fragments.APSTSViewPager;
import com.wbao.dianniu.utils.ActivityUtils;


/**
 * 我的收藏
 *
 * @author yejianjun
 */
public class MyCollectionActivity extends FragmentActivity implements
        ViewPager.OnPageChangeListener, OnClickListener {

    private ImageButton backButton;
    public CustomPagerSlidingTabStrip2 mAPSTS;
    public APSTSViewPager mVP;
    private CollectQaFragment fistFragment = null;
    private CollectKnowFragment secondFragment = null;

    private static final int VIEW_FIRST = 0;
    private static final int VIEW_SECOND = 1;

    private static final int VIEW_SIZE = 2;
    private FragmentAdapter adapter;
    private Context mContext;
    private TextView titleTV;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtils.setStatusBarColor(this);
        setContentView(R.layout.activity_my_qanswer);
        mContext = this;
        titleTV = findViewById(R.id.title);
        titleTV.setText(getResources().getString(R.string.my_collection));
        backButton = findViewById(R.id.back_button);
        backButton.setOnClickListener(this);

        mAPSTS = findViewById(R.id.qa_tab);
        mVP = findViewById(R.id.qa_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);

        adapter = new FragmentAdapter(getSupportFragmentManager());
        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                finish();
                break;

            default:

                break;
        }
    }

    private class FragmentAdapter extends FragmentStatePagerAdapter implements
            CustomPagerSlidingTabStrip2.CustomTabProvider {
        protected LayoutInflater mInflater;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
            mInflater = LayoutInflater.from(mContext);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_FIRST:
                        if (null == fistFragment)
                            fistFragment = CollectQaFragment.instance(mContext);
                        return fistFragment;
                    case VIEW_SECOND:
                        if (null == secondFragment)
                            secondFragment = CollectKnowFragment.instance(mContext);
                        return secondFragment;
                    default:
                        break;
                }
            }

            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public View getSelectTabView(int position, View convertView) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_select_tab_2,
                        null);
            }
            TextView tv = ViewHolder.get(convertView, R.id.tvTab);
            tv.setText(getPageTitle(position));
            return convertView;
        }

        @Override
        public View getDisSelectTabView(int position, View convertView) {
            if (convertView == null) {
                convertView = mInflater.inflate(
                        R.layout.custom_disselect_tab_2, null);
            }
            TextView tv = ViewHolder.get(convertView, R.id.tvTab);
            tv.setText(getPageTitle(position));
            return convertView;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case VIEW_FIRST:
                    return getResources().getString(R.string.qa_title);
                case VIEW_SECOND:
                    return getResources().getString(R.string.knowledge_title);
                default:
                    break;
            }

            return super.getPageTitle(position);
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


}
